package com.settlement.system.service.impl;

import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblQueryFieldConfigMapper;
import com.settlement.system.mapper.TblQueryTableConfigMapper;
import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.CommonPlugIn;
import com.settlement.system.model.entity.common.TblQueryTableConfig;
import com.settlement.system.reply.ObjectDataResponse;
import com.google.common.collect.Maps;
import com.settlement.system.service.ITblQueryTableConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联表查询表配置表 服务实现类
 * @author xiaochuan
 * @since 2022-01-18
 */
@Slf4j
@Service
public class TblQueryTableConfigServiceImpl extends BaseServiceImpl implements ITblQueryTableConfigService {

    @Resource
    private TblQueryTableConfigMapper tblQueryTableConfigMapper;

    @Resource
    private TblQueryFieldConfigMapper tblQueryFieldConfigMapper;

    @Override
    public BaseMapper init() {
        return tblQueryTableConfigMapper;
    }

    @Override
    public String getTableNameCnByTableNameZn(String tableNameZn) throws ServiceException {
        try {
            return tblQueryTableConfigMapper.getTableNameCnByTableNameZn(tableNameZn);
        } catch (Exception ex){
            log.error("getTableNameCnByTableNameZn by tableNameZn happen exception",ex);
            throw new ServiceException("getTableNameCnByTableNameZn by tableNameZn happen exception",ex);
        }
    }

    @Override
    public ObjectDataResponse pullTableNameList(Map<String, Object> model) {
        Map<String, Object> resultMap = Maps.newHashMap();
        try {
            resultMap.put("rows", getTableNameList());
            return new ObjectDataResponse<Map>(resultMap, ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception e) {
            log.error("页面获取渠道异常", e);
            return new ObjectDataResponse<Map>(null, ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    public List <CommonPlugIn> getTableNameList(){
        return tblQueryTableConfigMapper.pullTableNameList(new HashMap<>());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public <ModelType> Integer batchDelete(List<ModelType> list) throws ServiceException {
        for (ModelType modelType : list) {
            TblQueryTableConfig data = (TblQueryTableConfig) modelType;
            Map<String, Object> map = new HashMap<>();
            map.put("tableNameZn",data.getTableNameZn());
            tblQueryFieldConfigMapper.clear(map);
        }
        return super.batchDelete(list);
    }
}

